//
//  geyou_C1.m
//  K-Music
//
//  Created by CHUN on 16/1/15.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "geyou_C1.h"
#import "person_fansCell.h"
#import "GeYou_Model.h"

@interface geyou_C1 ()<UITableViewDataSource ,UITableViewDelegate>
@property (nonatomic ,strong) NSMutableArray * geyouDatas;
@property (nonatomic ,strong) UITableView *geyou_C1_TableView;
@end

@implementation geyou_C1

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB(241, 241, 241);
    self.navigationItem.title = @"我关注的歌友";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    [self creatUI];
    [self get_geyou];
}
- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -creatUI-
- (void)creatUI
{
    _geyou_C1_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT+49) style:UITableViewStylePlain];
    _geyou_C1_TableView.delegate = self;
    _geyou_C1_TableView.dataSource =self;
    _geyou_C1_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _geyou_C1_TableView.backgroundColor = RGB(241, 241, 241);
    [self.view addSubview:_geyou_C1_TableView];
    
}

- (void)get_geyou
{
    [MBProgressHUD showWithStatus:@"加载中..." toView:self.view];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString * userId = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:User_id]];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/friends/user_id/%@?page=0&size=20&sort=createdDate,DESC",userId];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功GET --> %@", dic);
        _geyouDatas = [NSMutableArray array];
        NSArray * arr = dic[@"body"];
        if (arr.count != 0) {
            NSArray * Datas = [GeYou_Model objectArrayWithKeyValuesArray:arr];
            _geyouDatas = [NSMutableArray arrayWithArray:Datas];
        }

        [self.geyou_C1_TableView reloadData];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showErrorWithStatus:@"数据获取失败" toView:self.view];
    }];
}

#pragma mark -UITableViewDataSource-

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _geyouDatas.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    person_fansCell * geyouCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (geyouCell == nil) {
        geyouCell = [[person_fansCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        [geyouCell creatUI];
    }
    
    GeYou_Model * data = [self.geyouDatas objectAtIndex:indexPath.row];
    
    [geyouCell.icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",data.avatar]]];
    
    geyouCell.icon.layer.cornerRadius= 30;
    geyouCell.icon.layer.masksToBounds = YES;
    
    geyouCell.titleName.text = data.nickName;
    geyouCell.fans_Attention.text = [NSString stringWithFormat:@"粉丝:%@ 人关注",data.fanNumber];
    geyouCell.singerLevelDesc.text = [NSString stringWithFormat:@"%@ ",data.singerLevelDesc] ;
    return geyouCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:YES];
}

@end
